package com.tsystems.skilltest;

import java.io.IOException;
import java.util.Properties;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * @author aesipov
 */
public class TransactionsSender
{
    private static Properties properties = PropertiesUtils.getProperties("skilltest.properties");

    public void send(String xmlPayload) throws IOException
    {
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
        System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
        System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "DEBUG");

        CloseableHttpClient httpclient = null;
        CloseableHttpResponse response = null;
        try
        {
            httpclient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(properties.getProperty("esb.service.url"));
            httpPost.setEntity(new StringEntity(xmlPayload, ContentType.APPLICATION_XML));
            response = httpclient.execute(httpPost);

            System.out.println(response.getStatusLine());
            HttpEntity entity2 = response.getEntity();
            EntityUtils.consume(entity2);
        }
        finally
        {
            if (response != null) {
                response.close();
            }

            if (response != null) {
                httpclient.close();
            }
        }
    }
}
